package com.zkdcloud.service;

import com.zkdcloud.domain.Category;
import com.zkdcloud.repository.CategoryDao;
import com.zkdcloud.util.RandomUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 分类service
 *
 * @author zk
 * @since 2018/5/2
 */
@Service
public class CategoryService {
    /**
     * static logger
     */
    private static Logger logger = LoggerFactory.getLogger(CategoryService.class);

    @Autowired
    private CategoryDao categoryDao;
    /**
     * 添加分类
     *
     * @param category c
     */
    public void addCategory(Category category){
        category.setId(RandomUtil.getUUID());
        category.setCreateTime(System.currentTimeMillis());

        if(StringUtils.isEmpty(category.getIconUrl())){
            category.setIconUrl("");
        }
        if(StringUtils.isEmpty(category.getName())){
            category.setName("");
        }
        if(StringUtils.isEmpty(category.getDescription())){
            category.setDescription("");
        }
        categoryDao.addCategory(category);
    }

    /**
     * 删除指定分类
     *
     * @param id id
     */
    public void remove(String id){
        categoryDao.removeById(id);
    }

    /**
     * 获取分类列表
     */
    public List<Category> getAllByBid(String bid){
        return categoryDao.getAll(bid);
    }

    /**
     * 根据id获取
     *
     * @param id id
     * @return category
     */
    public Category getOne(String id){
        return categoryDao.getById(id);
    }

    /**
     * 更新指定分类
     *
     * @param category c
     */
    public void update(Category category){
        categoryDao.update(category);
    }
}
